﻿using System.Collections.Generic;
using UcAsp.Net.IDAL;
using UcAsp.Net.Model;
namespace UcAsp.Net.BLL
{
    public class CategoriesBLL
    {
        private static readonly ICategoriesDAL dal = DALFactory.DataAccess.CreateCategories();
        private static readonly ISetFiledDAL dalfiled = DALFactory.DataAccess.CreateSetFiled();
        public List<Model.Categories> Select(int parentid, int pagesize, int pageindex, out int pagecount)
        {
            return dal.Select(parentid, pagesize, pageindex, out  pagecount);
        }
        public Model.Categories GetCategoriesById(int id)        
        {
            object obj = Comm.SpaceCache.GetCache("Categories"+id);
            if (obj == null)
            {
                Categories categories = dal.GetCategoriesById(id);
                if (categories == null)
                {
                    return null;
                }
                Comm.SpaceCache.CreateCache(dal.GetCategoriesById(id), "Categories" + id);   
            }
            return  (Model.Categories)Comm.SpaceCache.GetCache("Categories"+id);
        }
        
        public Model.Categories GetCategoriesByShowPath(string path)
        {

            object obj = Comm.SpaceCache.GetCache("Categories" + path);
            if (obj == null)
            {
                Comm.SpaceCache.CreateCache(dal.GetCategoriesByShowPath(path), "Categories" + path);
            }
            return (Model.Categories)Comm.SpaceCache.GetCache("Categories" + path);           
        }
        public Model.Categories Insert(Model.Categories model)
        {
            Categories result = dal.Insert(model);
            SetFiled titles = new SetFiled { CategoreiesID = model.ID, CnName = "标题", FiledName = "Title", InputType = "text", DataType = "string",Rank=10 };
            SetFiled categories = new SetFiled { CategoreiesID = model.ID, CnName = "分类", FiledName = "CategorieID", InputType = "select", DataType = "string", StoreText = "UcAsp.Net.BLL.CategoriesSelect,CategoriesSelect", Sort = model.ParentID.ToString(), Rank = 11 };
            SetFiled tag = new SetFiled { CategoreiesID = model.ID, CnName = "标签", FiledName = "Tag", InputType = "text", DataType = "string", Rank = 12 };
            SetFiled description = new SetFiled { CategoreiesID = model.ID, CnName = "概述", FiledName = "Description", InputType = "text", DataType = "string", Rank = 13 };
            SetFiled article = new SetFiled { CategoreiesID = model.ID, CnName = "内容", FiledName = "ArticleContent", InputType = "textarea", DataType = "string", Rank = 14 };
            SetFiled iconurl = new SetFiled { CategoreiesID = model.ID, CnName = "首图", FiledName = "IconUrl", InputType = "image", DataType = "string", Rank = 15 };
            SetFiled detakeyword = new SetFiled { CategoreiesID = model.ID, CnName = "SEO关键词", FiledName = "MetaKeyword", InputType = "text", DataType = "string", Rank = 16 };
            SetFiled metadescrition = new SetFiled { CategoreiesID = model.ID, CnName = "SEO描述", FiledName = "MetaDescrition", InputType = "text", DataType = "string", Rank = 17 };
            SetFiled viewtemp = new SetFiled { CategoreiesID = model.ID, CnName = "模板", FiledName = "ViewsTemp", InputType = "select", DataType = "string", StoreText = "UcAsp.Net.BLL.MessageThemes,MessageThemes", Rank = 19 };
            SetFiled showpath = new SetFiled { CategoreiesID = model.ID, CnName = "显示路径", FiledName = "ShowPath", InputType = "text", DataType = "string", Rank = 20 };

            dalfiled.Insert(titles);
            dalfiled.Insert(categories);
            dalfiled.Insert(tag);
            dalfiled.Insert(article);
            dalfiled.Insert(iconurl);
            dalfiled.Insert(detakeyword);
            dalfiled.Insert(metadescrition);
            dalfiled.Insert(description);
            dalfiled.Insert(viewtemp);
            dalfiled.Insert(showpath);
            Comm.SpaceCache.RemoveCache("Categories");
            return result;
        }
        public bool Update(Model.Categories model)
        {
            Comm.SpaceCache.RemoveCache("Categories" + model.ID);
            return dal.Update(model);
        }
        public bool Delete(int id)
        {
            Comm.SpaceCache.RemoveCache("Categories" + id);
            Comm.SpaceCache.RemoveCache("Categories");
            return dal.Delete(id);
        }
        public List<Model.Categories> SelectList(int parentid)
        {
            return dal.SelectList(parentid);
        }
        public List<Categories> SelectAll()
        {
            return dal.SelectAll();
        }
        public List<Categories> GetCategories()
        {
            object obj = Comm.SpaceCache.GetCache("Categories");
            if (obj == null)
            {
                Comm.SpaceCache.CreateCache(SelectAll(), "Categories");                
            }
            return (List<Categories>)Comm.SpaceCache.GetCache("Categories");
        }
        public List<Select> GetCategoriessComBox()
        {
            return dal.GetCategoriessComBox();
        }
        public List<Model.CategoriesTree> GetCategoriessTree(int parentid)
        {
            List<Model.CategoriesTree> list = new List<CategoriesTree>();
            list = dal.GetCategoriessTree(parentid, list, 0);
            return list;
        }
        public List<Model.CategoriesTree> GetCategoriessPath(int id)
        {
            return dal.GetCategoriessPath(id);
        }
    }
    public static class Categorie
    {
        public static Model.Categories GetCategorie(string id)
        {
            object obj = Comm.SpaceCache.GetCache("Categories" + id);
            if (obj == null)
            {
                Comm.SpaceCache.CreateCache(new CategoriesBLL().GetCategoriesById(int.Parse(id)), "Categories" + id);
            }
            return (Model.Categories)Comm.SpaceCache.GetCache("Categories" + id);
        }
    }
}